//
//  DynamicSignalCell.m
//  Portable Traffic Signal Assistor
//
//  Created by Ding on 8/13/12.
//  Copyright (c) 2012 TTU. All rights reserved.
//

#import "DynamicSignalCell.h"

@implementation DynamicSignalCell
static const CGFloat KEYBOARD_ANIMATION_DURATION = 0.3;
static const CGFloat MINIMUM_SCROLL_FRACTION = 0.2;
static const CGFloat MAXIMUM_SCROLL_FRACTION = 0.8;
static const CGFloat PORTRAIT_KEYBOARD_HEIGHT = 216;
static const CGFloat LANDSCAPE_KEYBOARD_HEIGHT = 162;


static const int TIMING_RING1_GRAPH_INITIAL_POSITION_X=5;
static const int TIMING_RING1_GRAPH_POSITION_Y=0;
static const int TIMING_RING1_GRAPH_HEIGHT=20;
static const int TIMING_RING2_GRAPH_INITIAL_POSITION_X=5;
static const int TIMING_RING2_GRAPH_POSITION_Y=22;
static const int TIMING_RING2_GRAPH_HEIGHT=20;

@synthesize signal=_signal;
@synthesize signalNameLabel=_signalNameLabel;
@synthesize activePhaseLabel=_activePhaseLabel;
@synthesize timingRing1GraphPositionX=_timingRing1GraphPositionX;
@synthesize timingRing2GraphPositionX=_timingRing2GraphPositionX;
@synthesize offsetPhaseTime=_offsetPhaseTime;
@synthesize labelList=_labelList;
@synthesize subActivePhaseLabel=_subActivePhaseLabel;
-(void) dealloc
{
    [_signal release];
    [_signalNameLabel release];
    [_activePhaseLabel release];
    [_labelList release];
    [_subActivePhaseLabel release];
    [super dealloc];
}
- (id)initWithSignal:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier signal:(Signal*)signal
{
    self.signal=signal;
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        // Initialization code
        self.signalNameLabel=[[[UILabel alloc] initWithFrame:CGRectMake(15, 2, 280-30, 30)] autorelease];
        self.signalNameLabel.backgroundColor=[UIColor whiteColor];
        self.signalNameLabel.numberOfLines=0;
        self.signalNameLabel.lineBreakMode=UILineBreakModeWordWrap;
        UIFont *cellFont = [ UIFont fontWithName: @"Arial" size: 12.0 ];
        self.signalNameLabel.minimumFontSize=5;
        //        self.signalNameLabel.font  = cellFont;
        self.signalNameLabel.layer.cornerRadius=8;
        self.signalNameLabel.text=self.signal.signalName;
        
        self.activePhaseLabel=[[[UILabel alloc] initWithFrame:CGRectMake(10, 35, 260, 42)] autorelease];
        self.activePhaseLabel.backgroundColor=[UIColor clearColor];
        self.activePhaseLabel.numberOfLines=0;
        self.activePhaseLabel.lineBreakMode=UILineBreakModeWordWrap;
        self.activePhaseLabel.minimumFontSize=5;
        self.activePhaseLabel.font  = cellFont;
        self.activePhaseLabel.layer.cornerRadius=0;
        [self drawSignalGraph];
        
        [self addSubview:self.signalNameLabel];
        [self addSubview:self.activePhaseLabel];
        
        //set offset phase time
        {
            self.offsetPhaseTime=0;
            int i=0;
            for (i=0; i<self.signal.ring1.phaseList.count; i++) {
                SignalPhase *phase=[self.signal.ring1.phaseList objectAtIndex:i];
                if ([self.signal.offsetPhase isEqualToString:phase.phaseID]) {
                    if ([self.signal.offsetBorE isEqualToString:@"Green End"]) {
                        self.offsetPhaseTime=self.offsetPhaseTime+[phase.greenTime doubleValue];
                        
                    }
                    break;
                }
                self.offsetPhaseTime=self.offsetPhaseTime+[phase.greenTime doubleValue]+[phase.yellowTime doubleValue]+[phase.allRedTime doubleValue];
            }
            if (i>=self.signal.ring1.phaseList.count) {
                self.offsetPhaseTime=0;
                for (i=0; i<self.signal.ring2.phaseList.count; i++) {
                    SignalPhase *phase=[self.signal.ring2.phaseList objectAtIndex:i];
                    if ([self.signal.offsetPhase isEqualToString:phase.phaseID]) {
                        if ([self.signal.offsetBorE isEqualToString:@"Green End"]) {
                            self.offsetPhaseTime=self.offsetPhaseTime+[phase.greenTime doubleValue];
                            
                        }
                        break;
                    }
                    self.offsetPhaseTime=self.offsetPhaseTime+[phase.greenTime doubleValue]+[phase.yellowTime doubleValue]+[phase.allRedTime doubleValue];
                }
                if (i>=self.signal.ring2.phaseList.count) {
                    self.offsetPhaseTime=0;
                }
            }
        }

    }
    return self;

}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated
{
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

- (void)drawSignalGraph
{
    self.subActivePhaseLabel=[[[UILabel alloc] initWithFrame:CGRectMake(0, 0, 260*2, 42)] autorelease];
//    self.timingRing1GraphPositionX=TIMING_RING1_GRAPH_INITIAL_POSITION_X;
//    self.timingRing2GraphPositionX=TIMING_RING2_GRAPH_INITIAL_POSITION_X;
    self.timingRing1GraphPositionX=0;
    self.timingRing2GraphPositionX=0;
    int timingGraphPositionY=TIMING_RING1_GRAPH_POSITION_Y;
    int timingGraphHeight=TIMING_RING1_GRAPH_HEIGHT;
    
    NSArray *ring1PhaseList=self.signal.ring1.phaseList;
    NSArray *ring2PhaseList=self.signal.ring2.phaseList;
    int ring1BarrierTime=0;
    for (int i=0; i<ring1PhaseList.count; i++) {
        SignalPhase *phase=[ring1PhaseList objectAtIndex:i];
        if ([phase.barrier intValue]==1) {
            int greenPhaseLength=round(260*[phase.greenTime doubleValue]/[self.signal.cycleLength intValue]);
            int yellowPhaseLength=round(260*[phase.yellowTime doubleValue]/[self.signal.cycleLength intValue]);
            int allRedPhaseLength=round(260*[phase.allRedTime doubleValue]/[self.signal.cycleLength intValue]);
            ring1BarrierTime=ring1BarrierTime+greenPhaseLength+yellowPhaseLength+allRedPhaseLength;
        }
    }
    ring1BarrierTime=ring1BarrierTime+self.timingRing1GraphPositionX;
    int ring2BarrierTime=0;
    for (int i=0; i<ring2PhaseList.count; i++) {
        SignalPhase *phase=[ring2PhaseList objectAtIndex:i];
        if ([phase.barrier intValue]==1) {
            int greenPhaseLength=round(260*[phase.greenTime doubleValue]/[self.signal.cycleLength intValue]);
            int yellowPhaseLength=round(260*[phase.yellowTime doubleValue]/[self.signal.cycleLength intValue]);
            int allRedPhaseLength=round(260*[phase.allRedTime doubleValue]/[self.signal.cycleLength intValue]);
            ring2BarrierTime=ring2BarrierTime+greenPhaseLength+yellowPhaseLength+allRedPhaseLength;
        }
    }
    ring2BarrierTime=ring2BarrierTime+self.timingRing2GraphPositionX;
    double barrierTime=0;
    if (ring1BarrierTime>=ring2BarrierTime) {
        barrierTime=ring1BarrierTime;
    }else{
        barrierTime=ring2BarrierTime;
    }
    int barrierPositionX=barrierTime;
    
    
    for (int i=0; i<ring1PhaseList.count; i++) {
        SignalPhase *phase=[ring1PhaseList objectAtIndex:i];
        if ([phase.barrier intValue]==2 && self.timingRing1GraphPositionX<barrierPositionX) {
            self.timingRing1GraphPositionX=barrierPositionX;
        }
        int greenPhaseLength=round(260*[phase.greenTime doubleValue]/[self.signal.cycleLength intValue]);
        int yellowPhaseLength=round(260*[phase.yellowTime doubleValue]/[self.signal.cycleLength intValue]);
        int allRedPhaseLength=round(260*[phase.allRedTime doubleValue]/[self.signal.cycleLength intValue]);
        UILabel *greenLabel = [[[UILabel alloc] initWithFrame:CGRectMake(self.timingRing1GraphPositionX,timingGraphPositionY,greenPhaseLength,timingGraphHeight)] autorelease];
        greenLabel.tag=100+i;
        greenLabel.text =[phase.phaseID stringByAppendingString:phase.phaseMovement];
        greenLabel.font = [UIFont boldSystemFontOfSize:12.0];
        greenLabel.textAlignment = UITextAlignmentCenter;
        greenLabel.backgroundColor = [UIColor greenColor];
        greenLabel.textColor=[UIColor darkGrayColor];
        //        [self addLabelResponder:greenLabel];
        [self.labelList addObject:greenLabel];
        [self.subActivePhaseLabel addSubview:greenLabel];
        self.timingRing1GraphPositionX=self.timingRing1GraphPositionX+greenPhaseLength;
        
        UILabel *yellowLabel = [[[UILabel alloc] initWithFrame:CGRectMake(self.timingRing1GraphPositionX,timingGraphPositionY,yellowPhaseLength,timingGraphHeight)] autorelease];
        yellowLabel.backgroundColor = [UIColor yellowColor];
        
        [self.labelList addObject:yellowLabel];
        [self.subActivePhaseLabel addSubview:yellowLabel];
        self.timingRing1GraphPositionX=self.timingRing1GraphPositionX+yellowPhaseLength;
        
        UILabel *allRedLabel = [[[UILabel alloc] initWithFrame:CGRectMake(self.timingRing1GraphPositionX,timingGraphPositionY,allRedPhaseLength,timingGraphHeight)] autorelease];
        allRedLabel.backgroundColor = [UIColor redColor];
        
        [self.labelList addObject:allRedLabel];
        [self.subActivePhaseLabel addSubview:allRedLabel];
        self.timingRing1GraphPositionX=self.timingRing1GraphPositionX+allRedPhaseLength;
    }
    {
        UILabel *barrierLabel = [[[UILabel alloc] initWithFrame:CGRectMake(barrierPositionX,timingGraphPositionY,5,timingGraphHeight)] autorelease];
        barrierLabel.backgroundColor = [UIColor blackColor];
        [self.labelList addObject:barrierLabel];
        [self.subActivePhaseLabel addSubview:barrierLabel];
    }
    
    self.timingRing1GraphPositionX=TIMING_RING1_GRAPH_INITIAL_POSITION_X;
    {
    
    //second cycle length labels
    for (int i=0; i<ring1PhaseList.count; i++) {
        SignalPhase *phase=[ring1PhaseList objectAtIndex:i];
        if ([phase.barrier intValue]==2 && self.timingRing1GraphPositionX<barrierPositionX) {
            self.timingRing1GraphPositionX=barrierPositionX;
        }
        int greenPhaseLength=round(260*[phase.greenTime doubleValue]/[self.signal.cycleLength intValue]);
        int yellowPhaseLength=round(260*[phase.yellowTime doubleValue]/[self.signal.cycleLength intValue]);
        int allRedPhaseLength=round(260*[phase.allRedTime doubleValue]/[self.signal.cycleLength intValue]);
        UILabel *greenLabel = [[[UILabel alloc] initWithFrame:CGRectMake(self.timingRing1GraphPositionX+261,timingGraphPositionY,greenPhaseLength,timingGraphHeight)] autorelease];
        greenLabel.tag=100+i;
        greenLabel.text =[phase.phaseID stringByAppendingString:phase.phaseMovement];
        greenLabel.font = [UIFont boldSystemFontOfSize:12.0];
        greenLabel.textAlignment = UITextAlignmentCenter;
        greenLabel.backgroundColor = [UIColor greenColor];
        greenLabel.textColor=[UIColor darkGrayColor];
        //        [self addLabelResponder:greenLabel];
        [self.labelList addObject:greenLabel];
        [self.subActivePhaseLabel addSubview:greenLabel];
        self.timingRing1GraphPositionX=self.timingRing1GraphPositionX+greenPhaseLength;
        
        UILabel *yellowLabel = [[[UILabel alloc] initWithFrame:CGRectMake(self.timingRing1GraphPositionX+261,timingGraphPositionY,yellowPhaseLength,timingGraphHeight)] autorelease];
        yellowLabel.backgroundColor = [UIColor yellowColor];
        
        [self.labelList addObject:yellowLabel];
        [self.subActivePhaseLabel addSubview:yellowLabel];
        self.timingRing1GraphPositionX=self.timingRing1GraphPositionX+yellowPhaseLength;
        
        UILabel *allRedLabel = [[[UILabel alloc] initWithFrame:CGRectMake(self.timingRing1GraphPositionX+261,timingGraphPositionY,allRedPhaseLength,timingGraphHeight)] autorelease];
        allRedLabel.backgroundColor = [UIColor redColor];
        
        [self.labelList addObject:allRedLabel];
        [self.subActivePhaseLabel addSubview:allRedLabel];
        self.timingRing1GraphPositionX=self.timingRing1GraphPositionX+allRedPhaseLength;
    }
        UILabel *barrierLabel = [[[UILabel alloc] initWithFrame:CGRectMake(barrierPositionX+261,timingGraphPositionY,5,timingGraphHeight)] autorelease];
        barrierLabel.backgroundColor = [UIColor blackColor];
        [self.labelList addObject:barrierLabel];
        [self.subActivePhaseLabel addSubview:barrierLabel];
    }
    
    
    timingGraphPositionY=TIMING_RING2_GRAPH_POSITION_Y;
    timingGraphHeight=TIMING_RING2_GRAPH_HEIGHT;
    
    for (int i=0; i<ring2PhaseList.count; i++) {
        SignalPhase *phase=[ring2PhaseList objectAtIndex:i];
        if ([phase.barrier intValue]==2 && self.timingRing2GraphPositionX<barrierPositionX) {
            self.timingRing2GraphPositionX=barrierPositionX;
        }
        int greenPhaseLength=round(260*[phase.greenTime doubleValue]/[self.signal.cycleLength intValue]);
        int yellowPhaseLength=round(260*[phase.yellowTime doubleValue]/[self.signal.cycleLength intValue]);
        int allRedPhaseLength=round(260*[phase.allRedTime doubleValue]/[self.signal.cycleLength intValue]);
        UILabel *greenLabel = [[[UILabel alloc] initWithFrame:CGRectMake(self.timingRing2GraphPositionX,timingGraphPositionY,greenPhaseLength,timingGraphHeight)] autorelease];
        greenLabel.tag=200+i;
        greenLabel.text =[phase.phaseID stringByAppendingString:phase.phaseMovement];
        greenLabel.font = [UIFont boldSystemFontOfSize:12.0];
        greenLabel.textAlignment = UITextAlignmentCenter;
        greenLabel.backgroundColor = [UIColor greenColor];
        greenLabel.textColor=[UIColor darkGrayColor];
        //        [self addLabelResponder:greenLabel];
        [self.labelList addObject:greenLabel];
        [self.subActivePhaseLabel addSubview:greenLabel];
        
        //        [self.labelList addObject:greenLabel];
        //        UILabel * tempLabel=[self.labelList objectAtIndex:self.labelList.count-1];
        //        [[self.labelList objectAtIndex:self.labelList.count-1] removeFromSuperview];
        
        self.timingRing2GraphPositionX=self.timingRing2GraphPositionX+greenPhaseLength;
        
        UILabel *yellowLabel = [[[UILabel alloc] initWithFrame:CGRectMake(self.timingRing2GraphPositionX,timingGraphPositionY,yellowPhaseLength,timingGraphHeight)] autorelease];
        yellowLabel.backgroundColor = [UIColor yellowColor];
        
        [self.labelList addObject:yellowLabel];
        [self.subActivePhaseLabel addSubview:yellowLabel];
        self.timingRing2GraphPositionX=self.timingRing2GraphPositionX+yellowPhaseLength;
        
        UILabel *allRedLabel = [[[UILabel alloc] initWithFrame:CGRectMake(self.timingRing2GraphPositionX,timingGraphPositionY,allRedPhaseLength,timingGraphHeight)] autorelease];
        allRedLabel.backgroundColor = [UIColor redColor];
        
        [self.labelList addObject:allRedLabel];
        [self.subActivePhaseLabel addSubview:allRedLabel];
        self.timingRing2GraphPositionX=self.timingRing2GraphPositionX+allRedPhaseLength;
    }
    {
        UILabel *barrierLabel = [[[UILabel alloc] initWithFrame:CGRectMake(barrierPositionX,timingGraphPositionY,5,timingGraphHeight)] autorelease];
        barrierLabel.backgroundColor = [UIColor blackColor];
        [self.labelList addObject:barrierLabel];
        [self.subActivePhaseLabel addSubview:barrierLabel];
    }
    
    
    self.timingRing2GraphPositionX=TIMING_RING2_GRAPH_INITIAL_POSITION_X;
    
    for (int i=0; i<ring2PhaseList.count; i++) {
        SignalPhase *phase=[ring2PhaseList objectAtIndex:i];
        if ([phase.barrier intValue]==2 && self.timingRing2GraphPositionX<barrierPositionX) {
            self.timingRing2GraphPositionX=barrierPositionX;
        }
        int greenPhaseLength=round(260*[phase.greenTime doubleValue]/[self.signal.cycleLength intValue]);
        int yellowPhaseLength=round(260*[phase.yellowTime doubleValue]/[self.signal.cycleLength intValue]);
        int allRedPhaseLength=round(260*[phase.allRedTime doubleValue]/[self.signal.cycleLength intValue]);
        UILabel *greenLabel = [[[UILabel alloc] initWithFrame:CGRectMake(self.timingRing2GraphPositionX+261,timingGraphPositionY,greenPhaseLength,timingGraphHeight)] autorelease];
        greenLabel.tag=200+i;
        greenLabel.text =[phase.phaseID stringByAppendingString:phase.phaseMovement];
        greenLabel.font = [UIFont boldSystemFontOfSize:12.0];
        greenLabel.textAlignment = UITextAlignmentCenter;
        greenLabel.backgroundColor = [UIColor greenColor];
        greenLabel.textColor=[UIColor darkGrayColor];
        //        [self addLabelResponder:greenLabel];
        [self.labelList addObject:greenLabel];
        [self.subActivePhaseLabel addSubview:greenLabel];
        
        //        [self.labelList addObject:greenLabel];
        //        UILabel * tempLabel=[self.labelList objectAtIndex:self.labelList.count-1];
        //        [[self.labelList objectAtIndex:self.labelList.count-1] removeFromSuperview];
        
        self.timingRing2GraphPositionX=self.timingRing2GraphPositionX+greenPhaseLength;
        
        UILabel *yellowLabel = [[[UILabel alloc] initWithFrame:CGRectMake(self.timingRing2GraphPositionX+261,timingGraphPositionY,yellowPhaseLength,timingGraphHeight)] autorelease];
        yellowLabel.backgroundColor = [UIColor yellowColor];
        
        [self.labelList addObject:yellowLabel];
        [self.subActivePhaseLabel addSubview:yellowLabel];
        self.timingRing2GraphPositionX=self.timingRing2GraphPositionX+yellowPhaseLength;
        
        UILabel *allRedLabel = [[[UILabel alloc] initWithFrame:CGRectMake(self.timingRing2GraphPositionX+261,timingGraphPositionY,allRedPhaseLength,timingGraphHeight)] autorelease];
        allRedLabel.backgroundColor = [UIColor redColor];
        
        [self.labelList addObject:allRedLabel];
        [self.subActivePhaseLabel addSubview:allRedLabel];
        self.timingRing2GraphPositionX=self.timingRing2GraphPositionX+allRedPhaseLength;
    }
    {
        UILabel *barrierLabel = [[[UILabel alloc] initWithFrame:CGRectMake(barrierPositionX+261,timingGraphPositionY,5,timingGraphHeight)] autorelease];
        barrierLabel.backgroundColor = [UIColor blackColor];
        [self.labelList addObject:barrierLabel];
        [self.subActivePhaseLabel addSubview:barrierLabel];
    }
    
    int offsetLabelPositionX=20;
    UILabel *offsetLabel = [[[UILabel alloc] initWithFrame:CGRectMake(TIMING_RING2_GRAPH_INITIAL_POSITION_X+offsetLabelPositionX,TIMING_RING1_GRAPH_POSITION_Y,2,50)] autorelease];
    offsetLabel.backgroundColor = [UIColor blueColor];
    
    [self.activePhaseLabel addSubview:self.subActivePhaseLabel];
    [self.activePhaseLabel addSubview:offsetLabel];

}

- (void)updateActivePhaseLabelWithMasterTime:(int)masterTime
{
    if ([self.signal.cycleLength intValue]==0) {
        return;
    }
    int time=round(([self.signal.offset intValue]-masterTime-self.offsetPhaseTime)*260/[self.signal.cycleLength intValue]);
    time=time+20;
    
    while (time>0) {
        time=time-260;
    }
    while (time<-260) {
        time=time+260;
    }
    CGRect frame=self.subActivePhaseLabel.frame;
    frame.origin=CGPointMake(time, frame.origin.y);
    self.subActivePhaseLabel.frame=frame;
    
}
@end
